package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* ClipView05
* */
class ClipView05 @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mPaint by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            color = context.resources.getColor(R.color.color_C840ED)
        }
    }

    private val mPath0 by lazy {
        Path()
    }

    private val mPath1 by lazy {
        Path()
    }

    private val mPath2 by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            translate(cx, cy)

            mPath0.apply {
                addArc(
                    -cx,
                    -cy * 2 + cy * 13 / 25,
                    cx,
                    0F + cy * 13 / 25,
                    0F,
                    360F
                )
            }

            mPath1.apply {
                fillType = Path.FillType.INVERSE_WINDING
                addCircle(
                    0F,
                    0F,
                    cx / 4,
                    Path.Direction.CW
                )

                addCircle(
                    0F,
                    0F,
                    cx / 8,
                    Path.Direction.CCW
                )
            }

            mPath2.apply {
                addArc(
                    -cx,
                    0F - cy * 13 / 25,
                    cx,
                    cy * 2 - cy * 13 / 25,
                    0F,
                    360F
                )
                clipPath(mPath0)
                clipPath(mPath1)
            }

            drawPath(mPath2, mPaint)
        }
    }
}